package com.zy.socket.chat2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 16:27
 **/
public class MyServerThread extends Thread {
    private Socket client;
    public MyServerThread(Socket client)
    {
        this.client = client;
    }
    @Override
    public void run() {
        System.out.println("来自客户端" + client.getRemoteSocketAddress() + "的连接！");
        try {
//3 读取来自客户端的消息
            DataInputStream dis = new DataInputStream(client.getInputStream());
            DataOutputStream dos = new DataOutputStream(client
                    .getOutputStream());
            while (true) {
                String str = dis.readUTF();
                System.out.println("收到客户端" + client.getRemoteSocketAddress()+ "消息:" + str);
                str = "服务端响应-" + str;
                dos.writeUTF(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
